package com.android.mensa;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.android.mensa.models.Day;
import com.android.mensa.models.DayAdapter;
import com.android.mensa.providers.IMenuProvider;
import com.android.mensa.providers.MenuProvider;

public class MainActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ViewPager pager = (ViewPager) findViewById(R.id.day_pager);
		TextView textView = (TextView) findViewById(R.id.no_menu);
		IMenuProvider provider = MenuProvider.getInstance();

		if (provider.getDaysCount() == 0) {

			textView.setVisibility(View.VISIBLE);
			pager.setVisibility(View.GONE);

		} else {
			AwesomePagerAdapter adapter = new AwesomePagerAdapter(this,
					provider);
			pager.setAdapter(adapter);
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
//		super.onSaveInstanceState(outState);
	}
	

	private class AwesomePagerAdapter extends PagerAdapter {

		private LayoutInflater mInflater;
		private IMenuProvider mProvider;
		private Context mContext;

		public AwesomePagerAdapter(Context context, IMenuProvider provider) {
			super();

			mContext = context;
			mProvider = provider;
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		}

		@Override
		public int getCount() {
			return mProvider.getDays().size();
		}

		/**
		 * Create the page for the given position. The adapter is responsible
		 * for adding the view to the container given here, although it only
		 * must ensure this is done by the time it returns from
		 * {@link #finishUpdate()}.
		 * 
		 * @param container
		 *            The containing View in which the page will be shown.
		 * @param position
		 *            The page position to be instantiated.
		 * @return Returns an Object representing the new page. This does not
		 *         need to be a View, but can be some other container of the
		 *         page.
		 */
		@Override
		public Object instantiateItem(View collection, int position) {

			ViewPager vp = (ViewPager) collection;
			Day day = mProvider.getDay(position);

			View dayView = mInflater.inflate(R.layout.day, null);
			TextView name = (TextView) dayView.findViewById(R.id.day_name);
			name.setText(day.getName());

			ListView list = (ListView) dayView.findViewById(R.id.meal_list);
			DayAdapter adapter = new DayAdapter(mContext, R.layout.menu, day
					.getMeals());
			list.setAdapter(adapter);
			vp.addView(dayView);

			return dayView;
		}

		/**
		 * Remove a page for the given position. The adapter is responsible for
		 * removing the view from its container, although it only must ensure
		 * this is done by the time it returns from {@link #finishUpdate()}.
		 * 
		 * @param container
		 *            The containing View from which the page will be removed.
		 * @param position
		 *            The page position to be removed.
		 * @param object
		 *            The same object that was returned by
		 *            {@link #instantiateItem(View, int)}.
		 */
		@Override
		public void destroyItem(View collection, int position, Object view) {
			((ViewPager) collection).removeView((View) view);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == ((View) object);
		}

		/**
		 * Called when the a change in the shown pages has been completed. At
		 * this point you must ensure that all of the pages have actually been
		 * added or removed from the container as appropriate.
		 * 
		 * @param container
		 *            The containing View which is displaying this adapter's
		 *            page views.
		 */
		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}
}